package com.zhongyouyun.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongyouyun.admin.model.*;
import com.zhongyouyun.admin.pojo.RolePojo;
import com.zhongyouyun.other.model.PageRequest;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.ModelUtils;
import com.zhongyouyun.utils.QueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.admin.service.RoleService;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: shenyi
 * @Date: 2021-09-23
 * @Description: 角色表
 */
@RestController
@RequestMapping("/admin/z_role")
@Api(value = "角色管理controller", tags = {"角色管理接口"})
public class RoleController {
    @Autowired
    private RoleService service;

    @PostMapping("/findPage")
    @ResponseBody
    @ApiOperation(value = "获取角色信息", notes = "通过角色获取角色信息")
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功", response = RoleModel.class)
    })
    public IPage<RolePojo> findPage(@RequestBody PageRequest pageRequest) {
        QueryWrapper<RolePojo> queryWrapper = new QueryWrapper<>();
        QueryUtils.fiterQuery(queryWrapper, pageRequest);
        Page page = new Page(pageRequest.getPageNum(), pageRequest.getPageSize());
//        logger.error("===============","=================");
        return service.selectPojoPage(page, queryWrapper);
    }

    @PostMapping("/findList")
    @ResponseBody
    @ApiOperation(value = "获取角色信息", notes = "通过角色获取角色信息")
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功", response = RoleModel.class)
    })
    public Result<List<RoleModel>> findList(@RequestBody PageRequest pageRequest) {
        QueryWrapper<RoleModel> queryWrapper = new QueryWrapper<>();
        QueryUtils.fiterQuery(queryWrapper, pageRequest);
        List<RoleModel> list = service.selectList(queryWrapper);
        if (list.size() > 0) {
            return Result.<List<RoleModel>>builder().message("成功").status(0).data(list).build();
        } else {
            return Result.<List<RoleModel>>builder().message("没有任何权限").status(1).build();
        }
    }

    @PostMapping("/findRoleMenus")
    @ResponseBody
    @ApiOperation(value = "获取角色信息", notes = "通过角色获取角色信息")

    public Result findRoleMenus(@RequestBody RoleModel roleModel) {
        List<MenuModel> list = service.findRoleMenus(roleModel);
        if (list.size() > 0) {
            Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
            resultMap.put("list", list);
            return Result.builder().message("成功").status(0).data(resultMap).build();
        } else {
            return Result.builder().message("没有任何权限").status(1).build();
        }

    }

    @PostMapping("/saveOrUpate")
    @ResponseBody
    @ApiOperation(value = "修改或者新增", notes = "通过ID判断修改或者新增")
    public Result saveOrUpate(@RequestBody RoleModel roleModel) {
        UserModel model = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        if (ObjectUtil.isNotEmpty(model.getId()) && roleModel.getId() != 0) {
            roleModel.setUpdateBy(model.getId().toString());
        } else {
            ModelUtils modelutils = new ModelUtils();
            modelutils.initModel(roleModel, model.getId().toString());
        }
        Boolean flag = service.saveOrUpdate(roleModel);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("成功").status(1).build();
        }
    }

    @PostMapping("/batchDelete")
    @ResponseBody
    @ApiOperation(value = "删除", notes = "通过ID删除")
    public Result batchDelete(@RequestBody List<UserModel> userModelList) {

        List<Long> list = new ArrayList<Long>();
        userModelList.forEach(userModel -> {
            list.add(userModel.getId());
        });
        Boolean flag = service.removeByIds(list);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("成功").status(1).build();
        }
    }

    @PostMapping("/saveRoleMenus")
    @ResponseBody
    @ApiOperation(value = "修改或者新增", notes = "通过ID判断修改或者新增")
    public Result saveRoleMenus(@RequestBody List<DictModel> dictModelList) {
        UserModel model = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        Boolean flag = service.saveRoleMenus(model, dictModelList);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("成功").status(1).build();
        }
    }

    @PostMapping("/findRoles")
    @ResponseBody
    @ApiOperation(value = "获取所有角色", notes = "获取所有角色")
    public Result findRoles(@RequestBody UserModel userModel) {
        List<Map<String, Object>> list = service.findRoles(userModel);
        if (list.size() > 0) {
            Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
            resultMap.put("list", list);
            return Result.builder().message("成功").status(0).data(resultMap).build();
        } else {
            return Result.builder().message("没有任何权限").status(1).build();
        }
    }
}
